<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言详情 - 留言板</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "Microsoft YaHei", "Segoe UI", sans-serif;
        }
        body {
            background-color: #f5f5f5;
            color: #333;
            line-height: 1.6;
        }
        a {
            color: #3498db;
            text-decoration: none;
            transition: color 0.3s;
        }
        a:hover {
            color: #2980b9;
            text-decoration: underline;
        }
        .container {
            width: 90%;
            max-width: 900px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            border-radius: 5px;
            margin-top: 20px;
            margin-bottom: 20px;
        }
        .header {
            border-bottom: 1px solid #eee;
            padding-bottom: 15px;
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .header h1 {
            color: #2c3e50;
            font-size: 24px;
        }
        .user-info {
            text-align: right;
            font-size: 14px;
        }
        .message-item {
            border-bottom: 1px solid #eee;
            padding: 20px 0;
        }
        .message-meta {
            color: #7f8c8d;
            font-size: 14px;
            margin-bottom: 15px;
        }
        .message-content {
            margin: 15px 0;
            line-height: 1.6;
            font-size: 16px;
            word-break: break-word;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 5px;
        }
        .reply-section {
            margin-top: 30px;
        }
        .reply-section h3 {
            color: #2c3e50;
            font-size: 18px;
            margin-bottom: 15px;
            padding-bottom: 8px;
            border-bottom: 1px solid #eee;
        }
        .reply-list {
            background: #f9f9f9;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .reply-item {
            padding: 10px 0;
            border-bottom: 1px dashed #eee;
        }
        .reply-item:last-child {
            border-bottom: none;
        }
        .reply-item span {
            font-weight: bold;
            color: #3498db;
        }
        .reply-form {
            margin-top: 30px;
            background: #f8f9fa;
            padding: 20px;
            border-radius: 5px;
        }
        .reply-form h3 {
            margin-bottom: 15px;
            color: #2c3e50;
            font-size: 18px;
        }
        .form-group {
            margin: 15px 0;
        }
        textarea {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: vertical;
            min-height: 100px;
            transition: border-color 0.3s;
        }
        textarea:focus {
            border-color: #3498db;
            outline: none;
        }
        .btn {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .btn:hover {
            background-color: #2980b9;
        }
        .reply-login-tip {
            margin-top: 20px;
            padding: 15px;
            background: #f8f9fa;
            text-align: center;
            border-radius: 4px;
            border-left: 4px solid #3498db;
        }
        .back-link {
            display: inline-block;
            margin-top: 20px;
            color: #7f8c8d;
        }
        .back-link:before {
            content: "← ";
        }
        @media (max-width: 768px) {
            .container {
                width: 95%;
                padding: 15px;
            }
            .header {
                flex-direction: column;
                align-items: flex-start;
            }
            .user-info {
                margin-top: 10px;
                text-align: left;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>留言详情</h1>
            
            <!-- 用户信息 -->
            <div class="user-info">
                {if condition="session('?user_id')"}
                    欢迎，{:session('username')} | 
                    <a href="{:url('user/logout')}">退出</a> | 
                    <a href="{:url('user/changePassword')}">修改密码</a>
                {else}
                    <a href="{:url('user/login')}">登录</a> | 
                    <a href="{:url('user/register')}">注册</a>
                {/if}
                | <a href="{:url('index/index')}">返回首页</a>
            </div>
        </div>

        <!-- 留言详情 -->
        <div class="message-item">
            <div class="message-meta">
                {if condition="$message.is_anonymous"}
                    <i>匿名用户</i>
                {else}
                    <b>{$message.user.username|default='游客'}</b>
                {/if}
                发表于 {$message.create_time}
                
                <!-- 操作按钮 -->
                {if condition="session('user_id') eq $message.user_id"}
                    | <a href="javascript:;" onclick="deleteMessage('{$message.id}')">删除</a>
                {/if}
                {if condition="session('?user_id')"}
                | <a href="javascript:;" onclick="likeMessage('{$message.id}')">
                    点赞(<span id="like-count-{$message.id}">{$message.likes|count}</span>)
                </a>
                {else}
                | <span title="请登录后点赞">点赞(<span id="like-count-{$message.id}">{$message.likes|count}</span>)</span>
                {/if}
            </div>
            <div class="message-content">{$message.content}</div>
        </div>
        
        <div class="reply-section">
            <!-- 回复列表 -->
            <h3>回复列表</h3>
            <div class="reply-list">
                {volist name="message.replies" id="reply" empty="<p>暂无回复</p>"}
                <div class="reply-item">
                    <span>{$reply.user.username}: </span>
                    {$reply.content}
                    {if condition="session('user_id') eq $reply.user_id"}
                        <a href="javascript:;" onclick="deleteReply('{$reply.id}')">删除</a>
                    {/if}
                </div>
                {/volist}
            </div>
            
            <!-- 发表回复 -->
            {if condition="session('?user_id')"}
            <div class="reply-form">
                <h3>发表回复</h3>
                <form action="{:url('reply/publish')}" method="post">
                    <input type="hidden" name="message_id" value="{$message.id}">
                    <div class="form-group">
                        <textarea name="content" placeholder="请输入回复内容"></textarea>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn">发表回复</button>
                    </div>
                </form>
            </div>
            {else}
            <div class="reply-login-tip">
                请<a href="{:url('user/login')}">登录</a>后发表回复
            </div>
            {/if}
        </div>
        
        <a href="{:url('index/index')}" class="back-link">返回留言列表</a>
    </div>

    <script>
        /**
         * 删除留言功能
         * 确认后提交表单到删除接口
         * @param {string} id - 要删除的留言ID
         */
        function deleteMessage(id) {
            if (confirm('确定要删除这条留言吗？')) {
                var form = document.createElement('form');
                form.method = 'post';
                form.action = "{:url('message/delete')}";
                var input = document.createElement('input');
                input.type = 'hidden';
                input.name = 'id';
                input.value = id;
                form.appendChild(input);
                document.body.appendChild(form);
                form.submit();
            }
        }

        /**
         * 删除回复功能
         * 确认后提交表单到删除接口
         * @param {string} id - 要删除的回复ID
         */
        function deleteReply(id) {
            if (confirm('确定要删除这条回复吗？')) {
                var form = document.createElement('form');
                form.method = 'post';
                form.action = "{:url('reply/delete')}";
                var input = document.createElement('input');
                input.type = 'hidden';
                input.name = 'id';
                input.value = id;
                form.appendChild(input);
                document.body.appendChild(form);
                form.submit();
            }
        }

        /**
         * 点赞功能
         * 使用fetch API异步提交点赞请求
         * @param {string} id - 要点赞的留言ID
         */
        function likeMessage(id) {
            // 检查登录状态
            if ("{:session('?user_id')}" !== "1") {
                alert('请先登录后再点赞');
                return;
            }
            
            fetch("{:url('message/like')}", {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: 'id=' + id
            })
            .then(response => response.json())
            .then(data => {
                if (data.status) {
                    document.getElementById('like-count-' + id).innerText = data.count;
                    alert(data.msg);
                } else {
                    alert(data.msg);
                }
            });
        }
    </script>
</body>
</html> 